package khleuven.mondzorg.app;

import khleuven.mondzorg.Logger;
import khleuven.mondzorg.R;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class DefinitionsActivity extends ActivityWithIllustrations {
	
	public static final String DEF_KEY = "definition";	
	private AutoCompleteTextView actv;	
	
	private String defString;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_definitions);
		
		Logger.logApp("aangekomen in definitionsactivity");
		fetchIntentExtras();
	}
	
	private void fetchIntentExtras(){
		Logger.logApp("aangekomen in fetchIntentExtras");
		Bundle b = getIntent().getExtras();
		if(b!=null){
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null");
			defString = b.getString(DEF_KEY);
			Logger.logApp("aangekomen in fetchIntentExtras - bundle niet null defstring:"+defString);
		}
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		initAutoCompleteTextView();
		if(defString != null){
			Logger.logApp("aangekomen in onResume - defString niet null defstring:"+defString);
            fillPage(true);
		}
	}
	
	private void initAutoCompleteTextView() {
		actv = (AutoCompleteTextView)findViewById(R.id.def_search);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.select_dialog_item, 
                dentalcare.getPreferredDefinitionsAsStringList());
        
        actv.setThreshold(1);
        actv.setAdapter(adapter);	
        
        actv.setOnItemClickListener(new OnItemClickListener() { 
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            	defString = arg0.getItemAtPosition(arg2).toString();
            	Logger.logApp("keuze: "+defString);
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(actv.getWindowToken(), 0);
                fillPage(false);
            }
        });
	}
	
	private void fillPage(boolean withdef){
		if(withdef){
			setDefinition();
		}
        setExplanation();
        setIllustration();
	}
	
	private void setDefinition(){
		actv.setText(defString);
		actv.dismissDropDown();
	}
	
	private void setExplanation(){
        String explanation = dentalcare.getDefinitionExplanation(defString);                
        TextView tvexp = (TextView)findViewById(R.id.txt_expl);
        setTextViewHTML(tvexp, explanation);
        tvexp.setMovementMethod(LinkMovementMethod.getInstance());
	}
	
	@Override
	protected void setIllustration(){
		illustrations = dentalcare.getDefinitionIllustrations(defString);
		super.setIllustration();
	}
	

	protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)	{
	    ClickableSpan clickable = new ClickableSpan() {
	          public void onClick(View view) {	        	  
	              defString = span.getURL();
	              Logger.logApp("clicked on: "+defString);
	              fillPage(true);
	          }
	    };
	    super.makeLinkClickable(strBuilder, span, clickable);
	}
}